﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SCOSub.FFMS2
{
    public partial class FFMS2Viewr : Form, IGetInfo
    {
        public FFMS2Viewr()
        {
            InitializeComponent();
        }
        FFMS2VideoProvider ff2;
        
        public void OpenFile(string filename)
        {
            try
            {
                ff2 = new FFMS2VideoProvider(filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                this.Dispose(true);
                throw ex;
            }

            Text = "";
            this.Size = new Size(ff2.Videoproperties.Width + 32, ff2.Videoproperties.Height + 91);
            pictureBox1.Image = ff2.GetFrameBitmap(0);
            trackBar1.Value = 0;
            trackBar1.Maximum = ff2.Videoproperties.NumFrames - 1;
            Text = trackBar1.Value.ToString() + " / " + (ff2.Videoproperties.NumFrames - 1).ToString();
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
      

        }       
        private int IndexCallback(int State, long Current, long Total, IntPtr Private)
        {
            this.Text = ((int)(Current * 100 / Total)).ToString() + " %";
            return 0; 
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (ff2 == null) return;
            ReadFrame();
          
        }
        private void ReadFrame()
        {
            Bitmap b = ff2.GetFrameBitmap(trackBar1.Value);
            if (b == null)
            {
                Text = "读取帧出错 " + trackBar1.Value.ToString();
                return;
            }
            pictureBox1.Image = b;
            int allms = 0;
            if (ff2.TimeCode.Length > trackBar1.Value)
                allms = (int)ff2.TimeCode[trackBar1.Value];
            int h = allms / 1000 / 3600;
            int min = (allms % 3600000) / 1000 / 60;
            int s = (allms % 60000) / 1000;
            int ms = allms % 1000;


            Text = trackBar1.Value.ToString() + " / " + (ff2.Videoproperties.NumFrames - 1).ToString() + "   " + string.Format("{0:0}:{1:00}:{2:00}.{3:000}", h, min, s, ms);
            pictureBox1.Refresh();
        }
        private void FFMS2Viewr_Resize(object sender, EventArgs e)
        {
            //if (ff2 == null) return;
            //if (pictureBox1.Image != null)
            //    pictureBox1.Image.Dispose();
            //pictureBox1.Image = ff2.GetFrameBitmap(trackBar1.Value, pictureBox1.ClientSize);
        }
        public bool getTimeTip_staff(out string n)
        {
            n = string.Format("+{0:D8}", trackBar1.Value);
            return true;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (ff2 == null)
            {
                base.OnKeyDown(e);
                return;
            }
            /*switch (e.KeyCode)
            {
                case Keys.Down:
                    {
                        int i = ff2.GetNextClosestKeyFrameIndex();
                        trackBar1.Value = i;
                        return;
                    }
                case Keys.Up:
                    {
                        int i = ff2.GetPrevClosestKeyFrameIndex();
                        trackBar1.Value = i;
                        return;
                    }
            }*/
            base.OnKeyDown(e);
        }
    }
}
